package com.lecture.java8.gui;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class AddView extends JFrame {

    private JPanel contentPane;
    private JTextField dateText;
    private JTextField nameText;
    private JTextField startText,lastText;

    private Service service=new Service();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    AddView frame = new AddView();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public AddView() {
        setTitle("Adding Schedule");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 80, 443, 310);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblNewLabel = new JLabel("date：");
        lblNewLabel.setBounds(112, 50, 43, 15);
        contentPane.add(lblNewLabel);

        dateText = new JTextField();
        dateText.setBounds(151, 47, 160, 21);
        contentPane.add(dateText);
        dateText.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("name：");
        lblNewLabel_1.setBounds(112, 93, 43, 15);
        contentPane.add(lblNewLabel_1);

        nameText = new JTextField();
        nameText.setBounds(151, 90, 160, 21);
        contentPane.add(nameText);
        nameText.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("starting：");
        lblNewLabel_2.setBounds(111, 134, 43, 15);
        contentPane.add(lblNewLabel_2);

        startText = new JTextField();
        startText.setBounds(151, 131, 160, 21);
        contentPane.add(startText);
        startText.setColumns(10);

        JLabel lblNewLabel_3 = new JLabel("lasting：");
        lblNewLabel_3.setBounds(111, 174, 43, 15);
        contentPane.add(lblNewLabel_3);

        lastText = new JTextField();
        lastText.setBounds(151, 171, 160, 21);
        contentPane.add(lastText);
        lastText.setColumns(10);

        //保存
        JButton saveBtn = new JButton("save");
        saveBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                int date = Integer.valueOf(dateText.getText());
                String name = nameText.getText();
                String starting = startText.getText();
                int lasting=Integer.valueOf(lastText.getText());
                if(date == 0 || "".equals(date)){
                    JOptionPane.showMessageDialog(contentPane, "Please enter the date", "System", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if(name == null || "".equals(name)){
                    JOptionPane.showMessageDialog(contentPane, "Please enter the name of task", "System", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if(starting == null || "".equals(starting)){
                    JOptionPane.showMessageDialog(contentPane, "Please enter the starting time", "System", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                if(lasting == 0 || "".equals(lasting)){
                    JOptionPane.showMessageDialog(contentPane, "Please enter the lasting time", "System", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                User user=new User();
                user.setDate(date);
                user.setName(name);
                user.setStarting(starting);
                user.setLasting(lasting);
                boolean flag = service.setTime("insert into timetable (`date`,`name`,`starting`,`lasting`) values ("+date+",'"+name+"','"+starting+"','"+lasting+"');");
                if(flag){
                    int l=Service.checkId(date,name).size();
                    for (int i=0;i<l;i++) {
                        //UserListView.load(Service.checkId(date,name).get(i));
                        UserListView.load(0);
                    }
                    dispose();
                    JOptionPane.showMessageDialog(contentPane, "Added successfully, refresh to view!");
                }else{
                    JOptionPane.showMessageDialog(contentPane, "Fail to add", "System", JOptionPane.WARNING_MESSAGE);
                }
                return;


            }
        });
        saveBtn.setBounds(151, 200, 74, 23);
        contentPane.add(saveBtn);

        //取消
        JButton cancleBtn = new JButton("cancel");
        cancleBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        cancleBtn.setBounds(237, 200, 74, 23);
        contentPane.add(cancleBtn);
    }

}
